﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Shove.Web.UI;

/// <summary>
/// 后台管理
/// </summary>
public class PageBase : System.Web.UI.Page
{
    public Site _Site;
    public Customer _Customer;

    public bool   isRequestLogin = true;                  // 是否需要登录
    public bool   SecondLevelPassword = false;            // 是否需要二级登录
    public string RequestLoginPage = "";                // 需要登录后，转跳到 Login.aspx 页面，登录后，会按此页面自动定位回来
    public bool   isAtFramePageLogin = true;              // 是否框架内的页面转跳登录。
    public string RequestCompetences = "";              // 需要的权限列表，用 [Administrator][Competence][...]... 表示
    public bool   isMasterSiteOnly = false;               // 设置此变量为 True，该页面只有总站才能操作

    public PageBase()
    {
    }

    protected override void OnLoad(EventArgs e)
    {
        #region 设置 Page Title、Keyword

        string PageTitle = "";
        try
        {
            this.Page.Title = PageTitle == "" ? _Site.SiteTitle : PageTitle;
        }
        catch { }

        HtmlMeta hm = new HtmlMeta();
        hm.Name = "keywords";
        //hm.Content = _Site.PageKeywords;

        try
        {
            this.Page.Header.Controls.Add(hm);
        }
        catch { }


        #endregion

        #region 根据URL，获取站点对象

        //_Site = new Site()[Shove._Web.Utility.GetUrlWithoutHttp()];
        _Site = new Site()["www.bq518.com"];        

        if (_Site == null)
        {
            PublicFunction.GoError(ErrorNumber.Unknow, "域名无效，限制访问", Shove._Web.Utility.GetUrlWithoutHttp());

            return;
        }

        #endregion

        #region 根据站点ID，获取用户对象

        _Customer = Customer.GetSessionCustomer(_Site.ID);

        if (isRequestLogin && (_Customer == null))
        {
            PublicFunction.GoLogin(RequestLoginPage, isAtFramePageLogin);

            return;
        }

        if (isRequestLogin && PublicFunction.GetOptionsAsBoolean("是否关闭系统", true) && (!_Customer.Competences[Competences.Administrator]))
        {
            Response.Write("系统升级中.........");
            Response.End();
        }

        if (SecondLevelPassword && PublicFunction.GetOptionsAsBoolean("我的办公室需验证二级密码", false))
        {
            SecondLevelPassword = true;
        }
        else
        {
            SecondLevelPassword = false;
        }

        string UserOfficeOK = (string)Shove._Web.Session.GetSessionAsString(Shove._Web.WebConfig.GetAppSettingsString("SystemPreFix") + "UserOffice", "");

        if (isRequestLogin && (_Customer != null) && SecondLevelPassword && (!_Customer.Competences[Competences.Administrator]) && (UserOfficeOK != PublicFunction.EncryptPassword("UserOfficeOKZXSystem")))
        {
            Response.Redirect("LoginOffice.aspx?SubPage=" + RequestLoginPage, true);

            return;
        }

        #endregion

        #region 判断权限
        if (isRequestLogin && (_Customer != null) && (RequestCompetences != "") && (!_Customer.Competences.IsOwnedCompetences(RequestCompetences)))
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", "PageBase");

            return;
        }

        #endregion

        #region 缓存

        //int SitePageCacheSeconds = Shove._Web.WebConfig.GetAppSettingsInt("SitePageCacheSeconds", -1);

        //if (SitePageCacheSeconds > 0)
        //{
        //    this.Response.Cache.SetExpires(DateTime.Now.AddSeconds(SitePageCacheSeconds));
        //    this.Response.Cache.SetCacheability(HttpCacheability.Server);
        //    this.Response.Cache.VaryByParams["*"] = true;
        //    this.Response.Cache.SetValidUntilExpires(true);
        //    this.Response.Cache.SetVaryByCustom("AdminPage");
        //}

        #endregion

        base.OnLoad(e);
    }
}